-- FacebookAgent
-- Created by chengb Dec/8/2015
-- 负责Facebook相关逻辑

module("FacebookAgent", package.seeall);

local PURE_FILE = "FacebookAgent";

-- 内部函数声明
local onFacebookConnected;
local onFacebookLoginError;
local onFBShareSucc;

-- 初始化
function init()
    -- 注册facebook账号登录的回调
    SdkMgr.registerOCCallback("onFacebookConnected", onFacebookConnected);

    -- 注册facebook账号登录的回调
    SdkMgr.registerOCCallback("onFacebookLoginError", onFacebookLoginError);

    -- 注册facebook分享成功回调
    SdkMgr.registerOCCallback("onFBShareSucc", onFBShareSucc);

    -- SDK 初始化
    local initOk = callOCFuncWithClass("FacebookWrap", "isInitOK");
    if not initOk then
        callOCFuncWithClass("FacebookWrap", "fbInit");
    end
end

-- facebook账号登录的回调
function onFacebookConnected(param)
    cclog("onFacebookConnected: param = " .. param);

    local arr = string.explode(param, "|");

    local account = arr[1];
    local token   = arr[2];

    -- 设置facebook账号登录信息
    local info = {
        ["account"] = account,
        ["extra"] = {
            ["3rdplatform"] = getFacebookChannelName(),
            ["token"]       = token,
        },
    };
    set3rdAccountLoginInfo(getFacebookChannelName(), info);

    -- 抛出facebook账号登录事件
    EventMgr.fire(event.FACEBOOK_CONNECTED, { ["loginInfo"] = info, });
end

-- facebook账号登录异常
function onFacebookLoginError(param)
    -- 弹提示
    showHint2(getLocStr("slime_note"), "Facebook login failed", true);
end

-- facebook分享成功的回调
function onFBShareSucc(shareType)
    shareType = tonumber(shareType);

    trace(PURE_FILE, "Facebook 分享成功，类型：%d", shareType);

    -- 抛出分享成功的事件
    EventMgr.fire(event.FB_SHARE_SUCC, { ["shareType"] = shareType, });
end

-- 请求登录facebook
function loginFacebook()
    callOCFuncWithClass("FacebookWrap", "loginFacebook");
end


